<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="950" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm"
				 label-width="110px"
				 label-position="left">
			<el-form-item label="企业客户名称" prop="company_name">
				<el-input v-model="form.company_name" placeholder="请输入企业客户名称，限制20个字符以内" clearable></el-input>
			</el-form-item>
			<el-form-item label="企业简称">
				<el-input v-model="form.company_short_name" placeholder="请输入企业简称，限制20个字符以内" clearable></el-input>
			</el-form-item>
			<el-form-item label="企业客户类型" prop="company_type">
				<el-select v-model="form.company_type" filterable style="width: 100%" clearable>
					<el-option v-for="item in companyTypeList" :key="item.dict_sub_val" :label="item.dict_sub_val" :value="item.dict_sub_val"/>
				</el-select>
			</el-form-item>
			<el-form-item label="联系人" prop="contact_user">
				<el-input v-model="form.contact_user" placeholder="请输入联系人，限制10个字符以内" clearable></el-input>
			</el-form-item>
			<el-form-item label="联系电话" prop="contact_mobile">
				<el-input v-model="form.contact_mobile" placeholder="请输入联系电话，数字，限制20个字符以内" clearable></el-input>
			</el-form-item>
			<el-form-item label="联系邮箱" prop="contact_email">
				<el-input v-model="form.contact_email" placeholder="请输入联系邮箱，限制50个字符" clearable></el-input>
			</el-form-item>
			<!--			<el-form-item label="渠道服务商" prop="channel_provider" clearable>-->
			<!--				<el-select v-model="form.channel_provider" filterable style="width: 100%">-->
			<!--					<el-option v-for="item in channelProviderList" :key="item.channel_provider" :label="item.channel_provider_name" :value="item.channel_provider"/>-->
			<!--				</el-select>-->
			<!--			</el-form-item>-->
			<el-form-item label="备注" prop="remark">
				<el-input v-model="form.remark" placeholder="请输入备注，限制500个字符"></el-input>
			</el-form-item>
			<el-form-item label="状态" prop="status">
				<el-switch v-model="form.status" :active-value="1" :inactive-value="0"></el-switch>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>

export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增企业客户',
				edit: '编辑企业客户',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				company_id: "",
				company_name: "",
				// channel_provider: "",
				company_short_name: "",
				company_type: "",
				contact_user: "",
				contact_mobile: "",
				contact_email: "",
				remark: "",
				status: "",
			},
			//验证规则
			rules: {
				company_name: [
					{required: true, message: '请输入企业客户名称'}
				],
				company_type: [
					{required: true, message: '请选择企业类型', trigger: 'change'}
				],
				// channel_provider: [
				// 	{required: true, message: '请选择渠道服务商'}
				// ],
				contact_user: [
					{required: true, message: '请输入联系人'}
				],
				contact_mobile: [
					{required: true, message: '请输入联系电话'}
				],
			},
			//所需数据选项
			statusList: [
				{status: 0, status_name: "停用"},
				{status: 1, status_name: "启用"},
			],
			//渠道
			channelProviderList: [],
			companyTypeList: [],
		}
	},
	mounted() {
		//获取渠道
		// this.getChannelProviderList()
		this.getCompanyTypeList()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		// getChannelProviderList() {
		// 	this.$API['company.company'].getChannelProviderList.get().then((res) => {
		// 		if (res.code == 0) {
		// 			this.channelProviderList = res.data;
		// 		}
		// 	}).catch((msg) => {
		// 		this.$alert(msg, "提示", {type: 'error'})
		// 	})
		// },
		getCompanyTypeList() {
			this.$API['company.company'].getCompanyTypeList.get().then((res) => {
				if (res.code == 0) {
					this.companyTypeList = res.data;
				}
			}).catch((msg) => {
				this.$alert(msg, "提示", {type: 'error'})
			})
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					this.$API['company.company'].store.post(this.form)
						.then((res) => {
							if (res.code == 0) {
								this.$emit('success', this.form, this.mode)
								this.$message.success("操作成功")
							} else {
								this.$alert(res.message, "提示", {type: 'error'})
							}
						})
						.catch((msg) => {
							this.$alert(msg, "提示", {type: 'error'})
						})
						.finally(() => {
							this.isSaveing = false;
						});
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		setData(data = []) {
			this.form.company_id = data.company_id
			this.form.company_name = data.company_name
			this.form.company_short_name = data.company_short_name
			this.form.company_type = data.company_type
			this.form.contact_user = data.contact_user
			this.form.contact_mobile = data.contact_mobile
			this.form.contact_email = data.contact_email
			// this.form.channel_provider = data.channel_provider
			this.form.status = parseInt(data.status)
			this.form.remark = data.remark
			//可以和上面一样单个注入，也可以像下面一样直接合并进去
			//Object.assign(this.form, data)
		}
	}
}
</script>

<style>
</style>
